{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "97272bb6",
   "metadata": {},
   "source": [
    "# 第二周学习笔记_python入门"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "88f5fc9d",
   "metadata": {},
   "source": [
    "# 一、上周回顾（了解python）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d49b57b5",
   "metadata": {},
   "source": [
    "## 1.1 什么是Python？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "96df48a4",
   "metadata": {},
   "source": [
    "* 定义：Python是一门动态解释性的强类型定义语言。  \n",
    "* 解释性：边写边译 \n",
    "* 动态：不用给如何变量指定数据类型，该语言会在你第一次赋值给变量时，在内部将数据类型记录下来 \n",
    "* 强类型定义语言：一旦一个变量被定义了某个数据类型，如果不经过强制转换，那么它就永远是这个数据类型  "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eb9a005d",
   "metadata": {},
   "source": [
    "## 1.2 python的特点"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f19f9bc",
   "metadata": {},
   "source": [
    "* “简单”“明确”\n",
    "* 开发效率高\n",
    "* 可移植性——所有Python程序无需修改就几乎可以在市场上所有的系统平台上运行\n",
    "* 可拓展性——部分程序用c或c++编写，可以在你的Python程序中使用它们\n",
    "* 可嵌入性——你可以把Python嵌入你的c/c++程序，从而向你的程序用户提供脚本功能"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1b8421e9",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "029b74df",
   "metadata": {},
   "source": [
    "# 二、基本工具安装与介绍"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa166e76",
   "metadata": {},
   "source": [
    "## 2.1 Pycharm安装与激活步骤"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42afa1a7",
   "metadata": {},
   "source": [
    "*（注意：安装专业版）*"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2e80dcf9",
   "metadata": {},
   "source": [
    "* 点击链接[github学生包](education.github.com)，点击选项“Github Student Developer Pack”\n",
    "* 进入页面后选择“get your pack”\n",
    "* 在进入的另一个页面中，点击\"get student benefits\"按钮\n",
    "* 进入网页后，点击“Add an email address”,填入学校邮箱（在企业微信---个人中心---邮箱）\n",
    "* 添加完邮箱后，在收到的邮箱中，点击\"verify email address\",然后完成github登录即可。\n",
    "* 回到[github学生包](https://education.github.com/pack/offers)，找到JETBRAINS的图标，点击下方蓝色文字“JetBrains”链接，进入JetBrains\n",
    "* 申请免费教育许可证: 点击Apply now，进入页面后填写信息提交\n",
    "* 在收到的邮箱中，根据邮箱内容操作，注册JetBrains账号\n",
    "* 下载完pycharm之后，创建项目后，点击help———register,可以看到专业版的免费只有30天。<br/>这时在下方对话框中，输入刚刚在JetBrains账号密码，提交后便可获得一年的免费使用 点击[pycharm下载链接](https://www.jetbrains.com/pycharm/)下载专业版\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d470090f",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2dfaecfe",
   "metadata": {},
   "source": [
    "## 2.2 jupyter notebook的使用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "82d6617f",
   "metadata": {},
   "source": [
    "* 点击 [jupyter notebook官网](https://jupyter.org/) 尝试体验\n",
    "* 可以用来做代码的系统管理，做每周学习内容的相关笔记\n",
    "* 如何在自己电脑打开jupyter notebook\n",
    "* 如已安装Anaconda，便可直接按（windows键+R）,输入cmd,在跳出对话框中输入jupyter notebook(系统便直接跳转到jupyter页面，但此路径为本地路径)\n",
    "* 根据jupyter页面的文件夹的提示，在本地电脑磁盘中找到相同的路径（Tips：二者显示的路径跟文件夹应为一样）\n",
    "* 在本地新建文件夹，后在网页中刷新便可编辑"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cd754f67",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "21e4aeb0",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "052d034a",
   "metadata": {},
   "source": [
    "# 三、 模块+函数（方法）=标准库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "52d6a7b5",
   "metadata": {},
   "source": [
    "## 3.1 观察标准库"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c0ee85aa",
   "metadata": {},
   "source": [
    "* 点击[了解标准库的某个特定的模块](https://docs.python.org/3/library/index.html)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f785bc2e",
   "metadata": {},
   "source": [
    "* 函数在模块里，模块在标准库里"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "55f9ee21",
   "metadata": {},
   "source": [
    "### （1）操作系统界面"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "7bf4cc31",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'C:\\\\Users\\\\thinkpad\\\\Class_python\\\\0906_week02'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# os模块 : 为python代码提供一种系统独立的方式与底层操作系统交互\n",
    "\n",
    "import os           \n",
    "os.getcwd()            # 返回当前工作目录"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4b9e06b4",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c84ed59",
   "metadata": {},
   "source": [
    "### （2）命令行参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "526ea2d9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'win32'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# sys模块 : 可以帮助你更多地了解运行解释器的系统\n",
    "\n",
    "import sys\n",
    "sys.platform"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd9c3ce5",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd81999a",
   "metadata": {},
   "source": [
    "### （3）时间和日期"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "id": "120e1ebd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2021, 9, 29)"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 准备工作 （准备要用到的模板和函数）\n",
    "# 调用日期函数\n",
    "import datetime\n",
    "datetime.date.today()                   # date.today会提供今天的日期"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0045adb5",
   "metadata": {},
   "source": [
    "* 也可以在datetime.today调用后追加一个要访问的属性来单独访问日、月、年。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "c04f28ea",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "29"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().day           # .day为添加的属性——今天的日"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "97d88b56",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.today().month         #今天的月"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e7b093d6",
   "metadata": {},
   "source": [
    "* 还可以调用date.isoformat函数并传入今天的日期，以另外一种形式显示，日期会由isoformat转换成一个字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "cc8b9c6b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2021-09-29'"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "datetime.date.isoformat(datetime.datetime.today())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15a6fd34",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "0ad65328",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "datetime.date(2021, 9, 27)"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 日期很容易被构造和格式化\n",
    "from datetime import date\n",
    "now = date.today()               \n",
    "now"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "804f3d1c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'09-27-21. 27 Sep 2021 is a Monday on the 27 day of September.'"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "now.strftime(\"%m-%d-%y. %d %b %Y is a %A on the %d day of %B.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "22046a09",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6934"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "birthday = date(2002,10,3)\n",
    "age = now - birthday            # 日期支持日历算法\n",
    "age.days"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "719aac4e",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6dc1191b",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 调用时间函数\n",
    "import time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "a64b21ee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=9, tm_mday=12, tm_hour=8, tm_min=41, tm_sec=2, tm_wday=6, tm_yday=255, tm_isdst=0)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.localtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "5bb6c26c",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "time.struct_time(tm_year=2021, tm_mon=9, tm_mday=12, tm_hour=1, tm_min=15, tm_sec=33, tm_wday=6, tm_yday=255, tm_isdst=0)"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.gmtime()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "6278e924",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1631408542.2444298"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "time.time()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "id": "c727175a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'21:52'"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入time模块后，调用strftime函数并指定你希望以什么方式显示时间——当前小时（%H）和分钟（%M）\n",
    "time.strftime(\"%H:%M\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9220f21f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Sunday AM'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 想知道今天星期几，是上午还是下午？\n",
    "time.strftime(\"%A %p\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5411a8cf",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "32a32160",
   "metadata": {},
   "source": [
    "### （4）内置函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8a0bd967",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 内置 dir() 和 help() 函数可用作处理大型模块的交互式辅助工具\n",
    "\n",
    "import struct\n",
    "dir()                 #显示模块空间中的名称（括号里面可以填什么）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29d69e0e",
   "metadata": {},
   "outputs": [],
   "source": [
    "dir(struct)              # 返回所有模块功能的列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "b188740a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The sum of 2 + 3 is 5'"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# format( *args , **kwargs ) 执行字符串格式化操作。调用此方法的字符串可以包含用大括号分隔的文字文本或替换字段 {}。\n",
    "# 每个替换字段包含位置参数的数字索引或关键字参数的名称。\n",
    "\"The sum of 2 + 3 is {0}\".format(2+3)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "531ec69e",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5205069c",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c206e52e",
   "metadata": {},
   "source": [
    "# 五、变量"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c392aca8",
   "metadata": {},
   "source": [
    "## 5.1 什么是变量？"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cca2b0cc",
   "metadata": {},
   "source": [
    "* 变量是储存信息（数字、文本_字符）的空间（小盒子）<br/>写出变量的产生，要赋予名字和意义"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bce5a7dc",
   "metadata": {},
   "source": [
    "* Python提供了所有常用的操作符，包括<,>,<=,>=,==,!=,以及=赋值操作符"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12fc2c36",
   "metadata": {},
   "source": [
    "## 5.2 标识符（变量名）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c77dd31d",
   "metadata": {},
   "source": [
    "* 第一个字符必须是字母表中字母或下划线 _ \n",
    "* 标识符的其他的部分由字母、数字和下划线组成\n",
    "* 标识符对大小写敏感"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d905449a",
   "metadata": {},
   "source": [
    "## 5.3 变量案例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "43d9e6c9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yanhaoxiang yanhaoxiang\n",
      "hejunlin yanhaoxiang\n"
     ]
    }
   ],
   "source": [
    "name = \"yanhaoxiang\"\n",
    "\n",
    "name2 = name\n",
    "print(name,name2)\n",
    "\n",
    "name = \"hejunlin\"\n",
    "\n",
    "print(name,name2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ab2b01b8",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee7e571d",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b39a3fc1",
   "metadata": {},
   "source": [
    "# 六、数字(number)类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98ffab99",
   "metadata": {},
   "source": [
    "python中数字有四种类型：整数、布尔型、浮点数和复数。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffad5e70",
   "metadata": {},
   "source": [
    "* int (整数), 如 1, 只有一种整数类型 int，表示为长整型，没有 python2 中的 Long。\n",
    "* bool (布尔), 如 True。\n",
    "* float (浮点数), 如 1.23、3E-2\n",
    "* complex (复数), 如 1 + 2j、 1.1 + 2.2j"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "afcc0b7c",
   "metadata": {},
   "source": [
    "## 6.1 数字（Number）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "6dd704bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 整数的类型（如：2、6、8、10）为“int”；而带有小数部分的整数的类型（如：5.0、2.5、1.6）为float。\n",
    "2+2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "814d4846",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.0"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 除法（/） 总会返回一个浮点值(即后面有小数点)\n",
    "15/5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "d4b710c6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 用（//）可以继续楼层分隔并获得整数结果\n",
    "17//3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6f49bd9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算余数，用（%）\n",
    "17%3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "27b4c54e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "98904144.64583333"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "9*4747398943/432"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d73d1130",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1025e31a",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e6dc56ac",
   "metadata": {},
   "source": [
    "# 七、字符串(string)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a34eeb4a",
   "metadata": {},
   "source": [
    "* python中单引号和双引号使用完全相同。\n",
    "* python三引号允许一个字符串跨多行，字符串中可以包含换行符、制表符以及其他特殊字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84c4f652",
   "metadata": {},
   "outputs": [],
   "source": [
    "word = '字符串'\n",
    "sentence = \"这是一个句子。\"\n",
    "paragraph = \"\"\"这是一个段落，\n",
    "可以由多行组成\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "e51e5764",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "这是一个多行字符串的实例\n",
      "多行字符串可以使用制表符\n",
      "TAB ( \t )。\n",
      "也可以使用换行符 [ \n",
      " ]。\n",
      "\n"
     ]
    }
   ],
   "source": [
    "para_str = \"\"\"这是一个多行字符串的实例\n",
    "多行字符串可以使用制表符\n",
    "TAB ( \\t )。\n",
    "也可以使用换行符 [ \\n ]。\n",
    "\"\"\"\n",
    "print (para_str)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0ea4d352",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b7dc17b",
   "metadata": {},
   "source": [
    "* 转换符(\"/\"为转义字符，告诉系统不是字符串而是个符号)\n",
    "* 反斜杠可以用来转义，使用r可以让反斜杠不发生转义。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "497c8e75",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"doesn't\""
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'doesn\\'t'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "49dcac1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "print('\\n')       # 输出空行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "16fd848f",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(r'\\n')      # 输出 \\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5608ef60",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c312b424",
   "metadata": {},
   "source": [
    "* 字符串可以用 + 运算符连接在一起，用 * 运算符重复。\n",
    "* Python 中的字符串有两种索引方式，从左往右以 0 开始，从右往左以 -1 开始。\n",
    "* Python中的字符串不能改变。\n",
    "* Python 没有单独的字符类型，一个字符就是长度为 1 的字符串。\n",
    "* 字符串的截取的语法格式如下：变量[头下标:尾下标:步长]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "d99315af",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123456789\n",
      "12345678\n",
      "1\n",
      "345\n",
      "3456789\n",
      "24\n",
      "123456789123456789\n",
      "123456789你好\n",
      "hello\n",
      "runoob\n"
     ]
    }
   ],
   "source": [
    "str='123456789'\n",
    " \n",
    "print(str)                 # 输出字符串\n",
    "print(str[0:-1])           # 输出第一个到倒数第二个的所有字符\n",
    "print(str[0])              # 输出字符串第一个字符\n",
    "print(str[2:5])            # 输出从第三个开始到第五个的字符\n",
    "print(str[2:])             # 输出从第三个开始后的所有字符\n",
    "print(str[1:5:2])          # 输出从第二个开始到第五个且每隔一个的字符（步长为2）\n",
    "print(str * 2)             # 输出字符串两次\n",
    "print(str + '你好')        # 连接字符串\n",
    "print('hello\\nrunoob')      # 使用反斜杠(\\)+n转义特殊字符-----换行"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccc54a73",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba22aba2",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 基本语法是通过 {} 和 : 来代替以前的 %。例如，'%03.2f'可以翻译为'{:03.2f}'.\n",
    "\n",
    "str.format()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "911e6372",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello world'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"{} {}\".format(\"hello\", \"world\")    # 不设置指定位置，按默认顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "db907628",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hello world'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"{0} {1}\".format(\"hello\", \"world\")  # 设置指定位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "fe0e7aa0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'world hello world'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"{1} {0} {1}\".format(\"hello\", \"world\")  # 设置指定位置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "8faed138",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网站名：菜鸟教程, 地址 www.runoob.com\n"
     ]
    }
   ],
   "source": [
    "# 设置参数：\n",
    "\n",
    "print(\"网站名：{name}, 地址 {url}\".format(name=\"菜鸟教程\", url=\"www.runoob.com\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8816d2d3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网站名：菜鸟教程, 地址 www.runoob.com\n"
     ]
    }
   ],
   "source": [
    "# 通过字典设置参数\n",
    "\n",
    "site = {\"name\": \"菜鸟教程\", \"url\": \"www.runoob.com\"}\n",
    "print(\"网站名：{name}, 地址 {url}\".format(**site))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "bc96618c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "网站名：菜鸟教程, 地址 www.runoob.com\n"
     ]
    }
   ],
   "source": [
    "# 通过列表索引设置参数\n",
    "\n",
    "my_list = ['菜鸟教程', 'www.runoob.com']\n",
    "print(\"网站名：{0[0]}, 地址 {0[1]}\".format(my_list))  # \"0\" 是必须的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "7eebcbf2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "大家好，我叫小邱同学，今年11岁，身高2.10m\n"
     ]
    }
   ],
   "source": [
    "# 字符串格式化（占位符）\n",
    "# %[对齐标志][宽度][.精度]类型 类型：%s字符 %d整数 %f小数 %%字符串%\n",
    "\n",
    "name = print(\"大家好，我叫%s，今年%d岁，身高%.2fm\"%(\"小邱同学\",11,2.1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "7a3beeed",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'2010-07-04 12:15:58'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用特定于类型的格式：\n",
    "\n",
    "import datetime\n",
    "d = datetime.datetime(2010, 7, 4, 12, 15, 58)\n",
    "'{:%Y-%m-%d %H:%M:%S}'.format(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "a341a610",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"repr() shows quotes: 'test1'; str() doesn't: test2\""
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 更换%s和%r：\n",
    "\n",
    "\"repr() shows quotes: {!r}; str() doesn't: {!s}\".format('test1', 'test2')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e63e7fdd",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1484751d",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f2fda89",
   "metadata": {},
   "source": [
    "# 八、if else 分别是什么？(条件：如果是...否则...)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e1d91442",
   "metadata": {},
   "outputs": [],
   "source": [
    "# if语句\n",
    "if条件：\n",
    "  if-语法块\n",
    "    \n",
    "if 条件：\n",
    "  if-语句块\n",
    "    \n",
    "else：\n",
    "  else-语法块\n",
    "    \n",
    "if 条件：\n",
    "  if-语句块\n",
    "    \n",
    "elif：\n",
    "  elif-语法块\n",
    "...\n",
    "else:\n",
    "    else-语句块\n",
    "\n",
    "# 可以继续嵌套；不要超过3层，最多5层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a37b5967",
   "metadata": {},
   "outputs": [],
   "source": [
    "# if、else和elif\n",
    "\n",
    "* if语句:求值为True的表达式\n",
    "\n",
    "* else语句：求值为false的表达式\n",
    "\n",
    "* elif语句：一是elif语句是“否则如果”，它总是跟在if或另一条elif语句后面。二是它提供了另一个条件，仅仅是在前面的条件为False时才检查该条件\n",
    "\n",
    "* else和elif的区别：else只有关键字和冒号，没有条件，直接下一行子句。elif后面要跟求True或False的条件，然后才有冒号和子句。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c8edf6aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入一个数字: -1\n",
      "负数\n"
     ]
    }
   ],
   "source": [
    "# 用户输入数字\n",
    " \n",
    "num = float(input(\"输入一个数字: \"))\n",
    "if num > 0:\n",
    "    print(\"正数\")\n",
    "elif num == 0:\n",
    "    print(\"零\")\n",
    "else:\n",
    "    print(\"负数\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "bbb9cd8c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "输入一个数字: 0\n",
      "零\n"
     ]
    }
   ],
   "source": [
    "# 内嵌 if 语句(对第一级的if进行筛分和解释)\n",
    " \n",
    "num = float(input(\"输入一个数字: \"))\n",
    "if num >= 0:\n",
    "    if num == 0:\n",
    "        print(\"零\")\n",
    "    else:\n",
    "        print(\"正数\")\n",
    "else:\n",
    "    print(\"负数\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "9c216222",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't sorrow!\n"
     ]
    }
   ],
   "source": [
    "import time\n",
    "today = time.strftime(\"%A\")\n",
    "if today == \"Saturday\":\n",
    "    print(\"Party!\")\n",
    "elif today == \"Sunday\":\n",
    "    print(\"Don't sorrow!\")\n",
    "else:\n",
    "    print(\"work,work,work.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b7015477",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e1fdd012",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "990f23c2",
   "metadata": {},
   "source": [
    "# 九、多行语句"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c5aaf2e7",
   "metadata": {},
   "source": [
    "* Python 通常是一行写完一条语句，但如果语句很长，我们可以使用反斜杠 \\ 来实现多行语句，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b0d11d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "total = item_one + \\\n",
    "        item_two + \\\n",
    "        item_three"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e5f55103",
   "metadata": {},
   "source": [
    "* 在 [], {}, 或 () 中的多行语句，不需要使用反斜杠 \\，例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c9e7f308",
   "metadata": {},
   "outputs": [],
   "source": [
    "total = ['item_one', 'item_two', 'item_three',\n",
    "        'item_four', 'item_five']"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e07c5c6c",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d42452c8",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35d4d43c",
   "metadata": {},
   "source": [
    "# 十、第一章课本代码练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "2de4cd70",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "时间是奇数，时间在odds的数据中\n"
     ]
    }
   ],
   "source": [
    "# 项目一，从奇数中选择对应的时间节点\n",
    "\n",
    "from datetime import datetime           # 第一个datetime（这是从中导入可重用代码的模块名字）第二个dtaetime（这是子模块的名字）\n",
    "                                    \n",
    "odds = [1,3,5,7,9,11,13,15,17,19,\n",
    "        21,23,25,27,29,31,33,35,37,                # odds（变量）是一个整数列表，向变量赋予一个数据结构\n",
    "        39,41,45,47,49,51,53,55,57,59]             # 与数组类似，列表可以保存任意类型的数据\n",
    "\n",
    "right_this_minute = datetime.today().minute\n",
    "\n",
    "if right_this_minute in odds:\n",
    "    print(\"时间是奇数，时间在odds的数据中\")\n",
    "else:\n",
    "    print(\"Not an odd minute\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f30c2118",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "6ad72247",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Not an odd second ————当前时间节点的秒钟： 14\n",
      "等待的随机时间： 1\n",
      "该时间为秒的单位的奇数时间 ————当前时间节点的秒钟： 15\n",
      "等待的随机时间： 2\n",
      "该时间为秒的单位的奇数时间 ————当前时间节点的秒钟： 17\n",
      "等待的随机时间： 1\n",
      "Not an odd second ————当前时间节点的秒钟： 18\n",
      "等待的随机时间： 4\n",
      "Not an odd second ————当前时间节点的秒钟： 22\n",
      "等待的随机时间： 3\n"
     ]
    }
   ],
   "source": [
    "# 项目二，拓展代码，运行“检查分钟代码”（利用python的\"for\"循环）\n",
    "# 可以把所要用到的import放在前面\n",
    "\n",
    "from datetime import datetime\n",
    "import random\n",
    "import time\n",
    "\n",
    "# odds是一个变量（奇数）\n",
    "# (内置数据函数：列表；也用[]包围)\n",
    "\n",
    "odds = [1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,45,47,49,51,53,55,57,59]\n",
    "\n",
    "# range 其实决定了for循环执行的次数\n",
    "# in操作符可以确定一个对象是否在另一个对象中\n",
    "# 冒号（：）引入一个必须向右缩进的新的代码组\n",
    "# for循环可以将下面5行代码重复执行五次\n",
    "\n",
    "for i in range(5):\n",
    "    # 由于range的存在 将执行5次\n",
    "    \n",
    "    # 下方为变量赋予值（将一个函数调用的结果赋给另一个新变量）\n",
    "    # second为变量\n",
    "    right_this_second = datetime.today().second\n",
    "    \n",
    "    #if else 循环嵌套的使用\n",
    "    if right_this_second in odds:\n",
    "        print(\"该时间为秒的单位的奇数时间\",\"————当前时间节点的秒钟：\",right_this_second)\n",
    "    else:\n",
    "        print(\"Not an odd second\",\"————当前时间节点的秒钟：\",right_this_second)\n",
    "        \n",
    "    # 增加等待时间\n",
    "    wait_time = random.randint(1,5)\n",
    "    print(\"等待的随机时间：\",wait_time)\n",
    "    time.sleep(wait_time)\n",
    "    # sleep为暂停执行，括号里面为“睡眠时间”"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bc53ef4",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "fcb28d14",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9 bottles of beer on the wall.\n",
      "9 bottles of beer.\n",
      "Take one down.\n",
      "Pass it around.\n",
      "8 bottles of beer on the walll.\n",
      "\n",
      "8 bottles of beer on the wall.\n",
      "8 bottles of beer.\n",
      "Take one down.\n",
      "Pass it around.\n",
      "7 bottles of beer on the walll.\n",
      "\n",
      "7 bottles of beer on the wall.\n",
      "7 bottles of beer.\n",
      "Take one down.\n",
      "Pass it around.\n",
      "6 bottles of beer on the walll.\n",
      "\n",
      "6 bottles of beer on the wall.\n",
      "6 bottles of beer.\n",
      "Take one down.\n",
      "Pass it around.\n",
      "5 bottles of beer on the walll.\n",
      "\n",
      "5 bottles of beer on the wall.\n",
      "5 bottles of beer.\n",
      "Take one down.\n",
      "Pass it around.\n",
      "4 bottles of beer on the walll.\n",
      "\n",
      "4 bottles of beer on the wall.\n",
      "4 bottles of beer.\n",
      "Take one down.\n",
      "Pass it around.\n",
      "3 bottles of beer on the walll.\n",
      "\n",
      "3 bottles of beer on the wall.\n",
      "3 bottles of beer.\n",
      "Take one down.\n",
      "Pass it around.\n",
      "2 bottles of beer on the walll.\n",
      "\n",
      "2 bottles of beer on the wall.\n",
      "2 bottles of beer.\n",
      "Take one down.\n",
      "Pass it around.\n",
      "1 bottle of beer on the walll.\n",
      "\n",
      "1 bottle of beer on the wall.\n",
      "1 bottle of beer.\n",
      "Take one down.\n",
      "Pass it around.\n",
      "No more bottles of beer on the wall.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# 项目三，“啤酒歌谣”\n",
    "\n",
    "# 将一个字符串（bottles）赋给一个新变量\n",
    "word = \"bottles\"\n",
    "\n",
    "# 从9倒数到0，循环指定次数，使用beer_num作为循环迭代变量\n",
    "for beer_num in range(9,0,-1):\n",
    "    print(beer_num,word,\"of beer on the wall.\")\n",
    "    print(beer_num,word,\"of beer.\")\n",
    "    print(\"Take one down.\")\n",
    "    print(\"Pass it around.\")\n",
    "    \n",
    "    #查看是否是最后一轮···如果是，结束歌词\n",
    "    if beer_num == 1:                                     # 两个等于号不能分开\n",
    "        print(\"No more bottles of beer on the wall.\")\n",
    "        \n",
    "    #如果不是，把下一瓶啤酒的编号记在另一个变量“new_num”中\n",
    "    else:\n",
    "        new_num = beer_num - 1\n",
    "        \n",
    "        # 如果我们要喝最后一瓶啤酒，修改word变量的值\n",
    "        if new_num == 1:\n",
    "            word = \"bottle\"\n",
    "        print(new_num,word,\"of beer on the wall.\")\n",
    "        \n",
    "    # 打印空行\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "70eedd4d",
   "metadata": {},
   "source": [
    "&emsp;"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
